package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.CurrencyBOInterface;
import org.cuatrovientos.erp.model.Currency;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller class form currency management
 * 
 * @author VB
 */
@Controller
public class CurrencyController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(CurrencyController.class);

	@Autowired
	private CurrencyBOInterface currencyBO;

	/**
	 * default constructor
	 */
	public CurrencyController() {
		logger.info("currency BEAN instantiated");
	}

	/**
	 * handles default /currency or /currency/list request
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/currency","/currency/list" )
	 */
	@RequestMapping({ "/currency", "/currency/list" })
	public ModelAndView currencyDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Currency> currencyList = currencyBO.selectAll();
		model.put("currencys", currencyList);
		return new ModelAndView("currencyList", model);
	}

	/**
	 * handles /currency/search request, redirected to default page
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/currency/search" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/currency/search" })
	public ModelAndView search(@RequestParam(value = "name", defaultValue = "", required = true) String name) {
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Currency> currencyList = currencyBO.search(name);
		model.put("currencys", currencyList);

		return new ModelAndView("currencyList", model);
	}

	/**
	 * handles delete currency, and redirect to currency default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/currency","/currency/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/currency/delete" })
	public String deletecurrency(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("delete " + id);

		currencyBO.deleteById(id);
		return "redirect:/currency";
	}

	/**
	 * handles new currency request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it: in this case "currency"
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/currency/new" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/currency/new" })
	public ModelAndView newcurrency() {
		logger.info("new currency ");
		Currency currency = new Currency();

		return new ModelAndView("Newcurrency", "currency", currency);
	}

	/**
	 * handles new currency request. In the jsp we are using spring's form:form
	 * tags Spring expects a model called by default 'command' or any name we
	 * set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/currency/new" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/currency/save" })
	public String newcurrency(@Valid Currency currency,BindingResult bindingResult) {
		logger.info("new currency " + currency.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("currency", currency);
			return "Newcurrency";
		}

		// If data is ok, insert and go on
		currencyBO.insert(currency);
		return "redirect:/currency";
	}

	/**
	 * handles delete currency, and redirect to currency default
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/currency","/currency/list" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/currency/detail" })
	public ModelAndView currencyDetail(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("Detail " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Currency currency = currencyBO.selectById(id);
		model.put("currency", currency);

		return new ModelAndView("currencyDetail", model);
	}

	/**
	 * handles currency update, first part: register loaded
	 * 
	 * @param model
	 * @return the name of the view to show
	 * @RequestMapping({"/currency/update" )
	 */
	@RequestMapping(method = RequestMethod.GET, value = { "/currency/update" })
	public ModelAndView update(@RequestParam(value = "id", defaultValue = "", required = true) int id) {
		logger.info("update " + id);
		Map<String, Object> model = new HashMap<String, Object>();
		Currency currency = currencyBO.selectById(id);
		model.put("currency", currency);
		return new ModelAndView("Updatecurrency", model);

	}

	/**
	 * handles save update currency request. In the jsp we are using spring's
	 * form:form tags Spring expects a model called by default 'command' or any
	 * name we set to it.
	 * 
	 * @return the name of the view to show
	 * @RequestMapping({"/currency/saveUpdate" )
	 */
	@RequestMapping(method = RequestMethod.POST, value = { "/currency/saveupdate" })
	public String saveUpdate(@Valid Currency currency,BindingResult bindingResult) {
		logger.info("update currency " + currency.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("currency", currency);
			return "Updatecurrency";
		}

		currencyBO.update(currency);
		return "redirect:/currency";
	}
}
